package com.hellogood.work.evaluate.server.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.hellogood.work.evaluate.server.db.DBHelper;
import com.hellogood.work.evaluate.shared.po.Task;

public class TaskDAO extends DBHelper {

	@Override
	public List<?> initializeObject(ResultSet rs) throws SQLException {
		List<Task> list = new ArrayList<Task>();
		if (rs != null) {
			while (rs.next()) {
				Task task = new Task();
				task.setTaskNo(rs.getString("taskNo") == null ? "" : rs.getString("taskNo").trim());
				task.setRemark(rs.getString("customerNo") == null ? "" : rs.getString("customerNo").trim());
				list.add(task);
			}
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<Task> getTaskList(String username, int start, int end, String field, String direct) {
		int num = end - start;
		String order = null;
		if (field != null && field != "" && direct != null && direct != "") {
			order = "DESC";
			if (direct == "ascending") {
				order = "ASC";
			}
		}
		String sql = "SELECT * FROM t_task ORDER BY createTime DESC LIMIT " + start + "," + num;
		if (order == null) {
			if (username != null&&!username.trim().equals("null")&&!username.trim().equals("")) {
				sql = "SELECT * FROM t_task WHERE username = '" + username + "' ORDER BY createTime DESC LIMIT " + start + "," + num;
			}
		} else {			
			if (username != null&&!username.trim().equals("null")&&!username.trim().equals("")) {
				sql = "SELECT * FROM t_task WHERE username = '" + username + "' ORDER BY " + field + " " + order + " LIMIT " + start + "," + num;
			}else{
				sql = "SELECT * FROM t_task ORDER BY " + field + " " + order + " LIMIT " + start + "," + num;
			}
		}

		return (List<Task>) super.query(sql);
	}

	public int getTotalTaskNumber(String username) {		
		String sql = "SELECT count(*) AS totalnumber FROM t_task";
		if (username != null) {
			sql = "SELECT count(*) AS totalnumber FROM t_task WHERE username = '" + username + "'";
		}
		return super.getTotalNumber(sql);
	}

}
